import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange, STData } from '@delon/abc/st';
import { SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { BasicTableComponent } from 'src/app/routes/commom';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-voucher-management',
  templateUrl: './voucher-management.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class VoucherManagementComponent extends BasicTableComponent {
  @ViewChild('auditModal', { static: false })
  auditModal!: any;
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  selectedRows: any[] = [];
  constructor(public service: FreightAccountService, private router: Router, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
      if (this.sf?.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf?.value.createTime?.[0] || null,
            end: this.sf?.value.createTime?.[1] || null
          }
        });
      }
      if (this.sf?.value.vctime) {
        Object.assign(requestOptions.body, {
          vctime: {
            start: this.sf?.value.vctime?.[0] || null,
            end: this.sf?.value.vctime?.[1] || null
          }
        });
      }
    }
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  // 导出
  exprot() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_fico_vch_export);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        vccode: {
          type: 'string',
          title: '凭证号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        cno: {
          type: 'string',
          title: '客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount()
          },
          default: ''
        },
        vctime: {
          title: '凭证时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        sourceCode: {
          type: 'string',
          title: '原始单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        sourceType: {
          type: 'string',
          title: '原始单类型',
          enum: [
            { value: '', label: '全部' },
            { value: 1, label: '收款' },
            { value: 2, label: '收款(退款)' },
            { value: 3, label: '应收费用' },
            { value: 4, label: '应收费用(负数)' },
            { value: 5, label: '应收核销' },
            { value: 6, label: '应收核销(负数)' },
            { value: 7, label: '应付费用' },
            { value: 8, label: '应付费用(负数)' },
            { value: 9, label: '应付核销' },
            { value: 10, label: '应付核销(负数)' },
            { value: 11, label: '分票开票' },
            { value: 12, label: '删除分票' },
            { value: 13, label: '付款' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        remarks: {
          type: 'string',
          title: '凭证摘要',
          ui: {
            placeholder: '请输入'
          }
        },
        vctype: {
          type: 'string',
          title: '凭证类型',
          enum: [
            {label: '全部', value:''},
            {label: 'WLW', value:'WLW'},
            {label: 'YH', value:'YH'},
            {label: 'KP', value:'KP'},
            {label: 'ZZ', value:'ZZ'},
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        // subid: {
        //   type: 'string',
        //   title: '科目',
        //   ui: {
        //     widget: 'dict-select',
        //     params: { dictKey: 'refund:apply:status' },
        //     placeholder: '请选择',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        drmoney: {
          type: 'string',
          title: '借方金额',
          ui: {
            placeholder: '请输入'
          }
        },
        crmoney: {
          type: 'string',
          title: '贷方金额',
          ui: {
            placeholder: '请输入'
          }
        },
        vcltdid: {
          type: 'string',
          title: '帐套',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择'
          }
        },
        sts: {
          type: 'string',
          title: '凭证状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'credential:status' },
            placeholder: '请选择'
          },
          default: ''
        },
        importncnotes: {
          type: 'string',
          title: 'NC凭证',
          ui: {
            placeholder: '请输入'
          }
        },
        vc2code: {
          type: 'string',
          title: '汇总凭证号',
          ui: {
            placeholder: '请输入'
          }
        },
        importnc: {
          type: 'string',
          title: '导入NC',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'refund:apply:status' },
            placeholder: '请选择'
          }
        },
        isvc2: {
          type: 'string',
          title: '是否汇总',
          enum: [
            { value: null, label: '全部' },
            { value: 0, label: '否' },
            { value: 1, label: '是' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: 60, className: 'text-center', fixed: 'left' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '凭证号', index: 'vccode', type: 'link', width: 200 },
      { title: '帐套', index: 'ltdId', width: 200, format: item => `${item.vcltdcode}-${item.vcltdname}` },
      { title: '凭证时间', index: 'vctime', type: 'date', width: 200 },
      { title: '凭证类型', index: 'vctype', width: 200 },
      // { title: '序号', index: 'invmoney', width: 200, format: _ => '1' },
      { title: '摘要', index: 'remarks', width: 300 },
      { title: '币种', index: 'currency', width: 100 },
      {
        title: '借方金额',
        index: 'drmoney',
        width: 120,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.drmoney }) }
      },
      {
        title: '贷方金额',
        index: 'crmoney',
        width: 120,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.crmoney }) }
      },
      { title: 'NC凭证', index: 'ncCode', width: 150 },
      { title: '汇总凭证号', index: 'vc2code', width: 150 },
      { title: '凭证状态', index: 'stsLabel', width: 120 },
      { title: '创建时间', index: 'createTime', width: 140 },
      // { title: '创建人', index: 'createUserId', width: 150 },
      {
        title: '操作',
        width: '130px',
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '浏览',
            click: (item: any) => this.router.navigate(['/financial-management/voucher-management/detail/' + item.id]),
            acl: { ability: ['FINANCIAL-VOUCHER-view'] }
          }
          // {
          //   text: '修改'
          // },
          // {
          //   text: '删除'
          // },
          // {
          //   text: '提交'
          // }
        ]
      }
    ];
  }
}
